home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / dfpp01.zip / MENUBAR.CPP < prev    next >
C/C++ Source or Header  |  1992-11-21  |  5KB  |  254 lines

  1. // ------------- menubar.cpp
  2.  
  3. #include <ctype.h>
  4. #include "desktop.h"
  5. #include "menubar.h"
  6. #include "menusel.h"
  7.  
  8. MenuBarItem::MenuBarItem(char *Title, MenuSelection **Ms,
  9.                          void (*MenuPrep)())
  10. {
  11.     title = new String(Title);
  12.     ms = Ms;
  13.     menuprep = MenuPrep;
  14.     popdown = NULL;
  15.     terminator = False;
  16. }
  17.  
  18. MenuBarItem::MenuBarItem(MenuTerminator)
  19. {
  20.     title = NULL;
  21.     terminator = True;
  22.     ms = NULL;
  23.     menuprep = NULL;
  24.     popdown = NULL;
  25. }
  26.  
  27. MenuBar::MenuBar( MenuBarItem *MenuItems, DFWindow *par) :
  28.             TextBox( par->ClientLeft(),    par->ClientTop()-1,
  29.                         1, par->ClientWidth(), par)
  30. {
  31.     windowtype = MenubarWindow;
  32.     menuitems = MenuItems;
  33.     SetAttribute(NOCLIP);
  34.     SetColors();
  35.     selection = -1;
  36.     ispoppeddown = False;
  37.     MenuBarItem *menu = menuitems;
  38.     menucount = 0;
  39.     oldfocus = NULL;
  40.     int off = 2;
  41.     SetTextLength(desktop.screen().Width()*2);
  42.     while (!menu->terminator)    {
  43.         int len = menu->title->Strlen()-1;
  44.         menu->x1 = off;
  45.         menu->x2 = off+len-1;
  46.         off += len+2;
  47.         String ttl("  ");
  48.         ttl += *(menu->title);
  49.         AddText(ttl);
  50.         int n = text->Strlen()-1;
  51.         (*text)[n] = '\0';
  52.         menu->popdown = new PopDown(this, menu->ms);
  53.         menu++;
  54.         menucount++;
  55.     }
  56. }
  57.  
  58. MenuBar::~MenuBar()
  59. {
  60.     // destructor code
  61.     MenuBarItem *menu = menuitems;
  62.     while (!menu->terminator)    {
  63.         delete menu->popdown;
  64.         menu++;
  65.     }
  66.     TextBox::CloseWindow();
  67. }
  68.  
  69. // -------- set the fg/bg colors for the window 
  70. void MenuBar::SetColors()
  71. {
  72.     colors.fg = BLACK;
  73.     colors.bg = LIGHTGRAY;
  74.     colors.sfg = BLACK;
  75.     colors.sbg = CYAN;
  76.     colors.ffg = BLACK;
  77.     colors.fbg = LIGHTGRAY;
  78.     colors.hfg = BLACK;
  79.     colors.hbg = LIGHTGRAY;
  80.     shortcutfg = RED;
  81. }
  82.  
  83. Bool MenuBar::SetFocus()
  84. {
  85.     if (oldfocus == NULL)
  86.         if (desktop.InFocus() != NULL)
  87.             if (desktop.InFocus()->State() != ISCLOSING)
  88.                 oldfocus = desktop.InFocus();
  89.     return TextBox::SetFocus();
  90. }
  91.  
  92. void MenuBar::ResetFocus()
  93. {
  94.     if (!ispoppeddown)    {
  95.         SetSelection(-1);
  96.         oldfocus = NULL;
  97.     }
  98.     TextBox::ResetFocus();
  99. }
  100.  
  101. void MenuBar::Paint()
  102. {
  103.     WriteShortcutLine(0, colors.fg, colors.bg);
  104.     if (selection != -1)    {
  105.         int x = menuitems[selection].x1;
  106.         int len = menuitems[selection].x2-x+2;
  107.         String sel = text->mid(len, x+selection);
  108.         DisplayShortcutField(sel, x, 0, colors.sfg, colors.sbg);
  109.     }
  110. }
  111.  
  112. void MenuBar::LeftButton(int mx, int)
  113. {
  114.     mx -= Left();
  115.     MenuBarItem *menu = menuitems;
  116.     int sel = 0;
  117.     while (!menu->terminator)    {
  118.         if (mx >= menu->x1 && mx <= menu->x2)    {
  119.             if (selection != sel || !ispoppeddown)    {
  120.                 if (ispoppeddown)    {
  121.                     PopDown *pd = menuitems[selection].popdown;
  122.                     if (pd->isOpen())
  123.                         pd->CloseMenu(False);
  124.                 }
  125.                 Select(sel);
  126.             }
  127.             return;
  128.         }
  129.         sel++;
  130.         menu++;
  131.     }
  132.     if (selection == -1)
  133.         SetSelection(0);
  134. }
  135.  
  136. void MenuBar::SetSelection(int sel)
  137. {
  138.     selection = sel;
  139.     Paint();
  140. }
  141.  
  142. void MenuBar::Select(int sel)    {
  143.     selection = sel;
  144.     Select();
  145. }
  146.  
  147. void MenuBar::Select()
  148. {
  149.     Paint();
  150.     ispoppeddown = True;
  151.     MenuBarItem &mb = *(menuitems+selection);
  152.     int lf = Left() + mb.x1;
  153.     int tp = Top()+1;
  154.     if (mb.menuprep != NULL)
  155.         (*mb.menuprep)();
  156.     mb.popdown->OpenMenu(lf, tp);
  157. }
  158.  
  159. Bool MenuBar::AcceleratorKey(int key)
  160. {
  161.     MenuBarItem *menu = menuitems;
  162.     while (!menu->terminator)    {
  163.         PopDown *pd = menu->popdown;
  164.         if (pd->AcceleratorKey(key))
  165.             return True;
  166.         menu++;
  167.     }
  168.     return False;
  169. }
  170.  
  171. Bool MenuBar::ShortCutKey(int key)
  172. {
  173.     int altkey = desktop.keyboard().AltConvert(key);
  174.     MenuBarItem *menu = menuitems;
  175.     int sel = 0;
  176.     while (!menu->terminator)    {
  177.         int off = menu->title->FindChar(SHORTCUTCHAR);
  178.         if (off != -1)    {
  179.             String &cp = *(menu->title);
  180.             int c = cp[off+1];
  181.             if (tolower(c) == altkey)    {
  182.                 SetFocus();
  183.                 Select(sel);
  184.                 return True;
  185.             }
  186.         }
  187.         sel++;
  188.         menu++;
  189.     }
  190.     return False;
  191. }
  192.  
  193. void MenuBar::Keyboard(int key)
  194. {
  195.     if (AcceleratorKey(key))
  196.         return;
  197.     if (!ispoppeddown && ShortCutKey(key))
  198.         return;
  199.     switch (key)    {
  200.         case F10:
  201.             if (ispoppeddown)
  202.                 break;
  203.             if (this != desktop.InFocus())    {
  204.                 if (selection == -1)
  205.                     selection = 0;
  206.                 SetFocus();
  207.                 break;
  208.             }
  209.             // ------ fall through
  210.         case ESC:
  211.             ispoppeddown = False;
  212.             SetSelection(-1);
  213.             if (oldfocus != NULL)
  214.                 oldfocus->SetFocus();
  215.             else
  216.                 parent->SetFocus();
  217.             break;
  218.         case FWD:
  219.             selection++;
  220.             if (selection == menucount)
  221.                 selection = 0;
  222.             if (ispoppeddown)
  223.                 Select();
  224.             else
  225.                 Paint();
  226.             break;
  227.         case BS:
  228.             if (selection == 0)
  229.                 selection = menucount;
  230.             --selection;
  231.             if (ispoppeddown)
  232.                 Select();
  233.             else
  234.                 Paint();
  235.             break;
  236.         case '\r':
  237.             if (selection != -1)
  238.                 Select();
  239.             break;
  240.         case ALT_F6:
  241.             TextBox::Keyboard(key);
  242.             break;
  243.         default:
  244.             break;
  245.     }
  246. }
  247.  
  248. void MenuBar::ParentSized(int xdif, int)
  249. {
  250.     Size(Right()+xdif, Bottom());
  251. }
  252.  
  253.  
  254.